<?php

namespace app\common\model\activity;

use app\common\utils\Utils;
use think\Model;
use traits\model\SoftDelete;

class Item extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'activity_item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'is_show_text',
        'starttime_text',
        'endtime_text',
        'type_text',
        'date_text',
        'image_text'
    ];


    protected static function init()
    {
        Item::beforeWrite(function ($item) {
            $post = request()->post('row/a');
            $item->start_time = strtotime($post['start_time']);
            $item->endtime = strtotime($post['endtime']);
        });
    }
    public function getImageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');

        return cdnurl($value, true);
    }

    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');

        return Utils::time_format($value);
    }
    public function getDateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');

        return Utils::time_format($value, 'Y-m-d');
    }
    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');

        return Utils::time_format($value);
    }
    public function getTypeTextAttr($value, $data) {
        $start = $data['start_time'];
        $end = $data['endtime'];

        //判断所处状态
        $now = time();
        if ($now < $start) {
            return [0, '未开始'];
        } else if ($start <= $now && $now <= $end) {
            return [1, '立即报名'];
        } else {
            return [2, '已截止'];
        }
    }

    public function getIsShowList()
    {
        return ['0' => __('Is_show 0'), '1' => __('Is_show 1')];
    }


    public function getIsShowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_show']) ? $data['is_show'] : '');
        $list = $this->getIsShowList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
